package com.example.customer01.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class PerformanceAspect {
    private static final Logger logger = LoggerFactory.getLogger(PerformanceAspect.class);
    private static final long SLOW_EXECUTION_THRESHOLD = 1000; // 1秒

    @Pointcut("execution(* com.example.customer01.service.*.*(..))")
    public void serviceLayer() {}

    @Around("serviceLayer()")
    public Object monitorPerformance(ProceedingJoinPoint joinPoint) throws Throwable {
        long startTime = System.currentTimeMillis();
        Object result = joinPoint.proceed();
        long endTime = System.currentTimeMillis();
        long executionTime = endTime - startTime;

        String methodName = joinPoint.getSignature().getName();
        String className = joinPoint.getTarget().getClass().getSimpleName();

        if (executionTime > SLOW_EXECUTION_THRESHOLD) {
            logger.warn("性能警告: {}.{} 执行时间过长: {}ms", className, methodName, executionTime);
        } else {
            logger.debug("方法执行时间: {}.{} - {}ms", className, methodName, executionTime);
        }

        return result;
    }
} 